package com.zjw.hyphenateui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.havi.bean.MessageBean;
import com.example.havi.helper.GlideHelper;
import com.example.havi.helper.MessageTypes;
import com.example.havi.util.LogUtil;

import com.tencent.imcore.SessionExt;
import com.tencent.imsdk.IMLiteBridge;
import com.tencent.imsdk.TIMConversation;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.imsdk.TIMElem;
import com.tencent.imsdk.TIMElemType;
import com.tencent.imsdk.TIMFriendshipManager;
import com.tencent.imsdk.TIMMessage;
import com.tencent.imsdk.TIMTextElem;
import com.tencent.imsdk.TIMUserProfile;
import com.tencent.imsdk.TIMValueCallBack;
import com.tencent.imsdk.ext.message.TIMConversationExt;
import com.tencent.imsdk.ext.message.TIMManagerExt;
import com.tencent.imsdk.ext.message.TIMMessageDraft;
import com.tencent.imsdk.ext.message.TIMMessageExt;
import com.zjw.hyphenateui.R;
import com.zjw.hyphenateui.bean.MyConversation;
import com.zjw.hyphenateui.util.DateUtils;
import com.zjw.hyphenateui.util.GsonUtils;
import com.zjw.hyphenateui.widget.easyadapter.BaseViewHolder;
import com.zjw.hyphenateui.widget.easyadapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class ConversationAdapter extends RecyclerArrayAdapter<MyConversation> {

    private String TAG = "mango";

    public ConversationAdapter(Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(getContext()).inflate(R.layout.item_conversation, parent, false));
    }

    class ViewHolder extends BaseViewHolder<MyConversation> {
        TextView unread_msg_number_tv, name_tv, time_tv, mentioned_tv, message_tv, btnDelete;
        ImageView avatar_iv, msg_state_iv;
        View root;

        public ViewHolder(View itemView) {
            super(itemView);
            avatar_iv = itemView.findViewById(R.id.avatar);
            unread_msg_number_tv = itemView.findViewById(R.id.unread_msg_number);
            name_tv = itemView.findViewById(R.id.name);
            time_tv = itemView.findViewById(R.id.time);
            msg_state_iv = itemView.findViewById(R.id.msg_state);
            mentioned_tv = itemView.findViewById(R.id.mentioned);  //群聊显示有人@我 这里不使用
            message_tv = itemView.findViewById(R.id.message);
            btnDelete = itemView.findViewById(R.id.btnDelete);
            root = itemView.findViewById(R.id.root);
        }

        @Override
        public void setData(final MyConversation conversation) {
            updateUI_2(conversation);   //
            btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    TIMManagerExt.getInstance().deleteConversation(
                            conversation.getLastMessage().getConversation().getType(),
                            conversation.getLastMessage().getConversation().getPeer());
                    remove(conversation);
                }
            });

            root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (clickListener != null) {
                        clickListener.onItemClick(view, getDataPosition());
                    }
                }
            });
        }


        private void updateUI_2(MyConversation conversation) {
            //消息未读数
            if (conversation.getUnReadCount() > 0) {
                unread_msg_number_tv.setText(String.valueOf((int) conversation.getUnReadCount()));
                unread_msg_number_tv.setVisibility(View.VISIBLE);
            } else {
                unread_msg_number_tv.setVisibility(View.INVISIBLE);
            }

//
//                if (lastMessage.getChatType() == EMMessage.ChatType.GroupChat) {    //群聊会话
//                GlideHelper.INSTANCE.loadCircleImage(avatar_iv, messageBean.getGroupAvatar() + "");
//                name_tv.setText(messageBean.getGroupName() + "");
//                    EMGroup group = EMClient.getInstance().groupManager().getGroup(conversation.conversationId());  //获取群信息
//            } else
//                if (lastMessage.getChatType() == EMMessage.ChatType.ChatRoom) {    //聊天室会话
//                avatar_iv.setImageResource(R.mipmap.ic_launcher);
//                EMChatRoom room = EMClient.getInstance().chatroomManager().getChatRoom(conversation.conversationId());
//                name_tv.setText(room != null && !TextUtils.isEmpty(room.getName()) ? room.getName() : conversation.conversationId());
//            } else {
//
// 单聊会话
//                if (lastMessage.direct() == EMMessage.Direct.RECEIVE) {
            //会话中最后一条消息是接收的取avatar和nickName
//                    GlideHelper.INSTANCE.loadCircleImage(avatar_iv, messageBean.getAvatar() + "");
//                    name_tv.setText(messageBean.getNickName() + "");
//                } else {
//                    //会话中最后一条消息是自己发出去的 取toAvatar和toNickName
//                    GlideHelper.INSTANCE.loadCircleImage(avatar_iv, messageBean.getToAvatar() + "");
//                    name_tv.setText(messageBean.getToNickName() + "");
//                }
//            }

            GlideHelper.INSTANCE.loadCircleImage(avatar_iv, conversation.getFaceUrl());
            name_tv.setText(conversation.getNickName());
            String digest = "";

            TIMElem element = conversation.getLastMessage().getElement(0);
            TIMElemType type = element.getType();
            switch (type)
            {
                case Location:
//                    if (lastMessage.direct() == EMMessage.Direct.RECEIVE) {
//                        digest = "[" + lastMessage.getFrom() + "的位置]";
//                    } else {
//                        digest = "[我的位置]";
//                    }
                    break;
                case Image:
                    digest = "[图片]";
                    break;
                case Sound:
                    digest = "[语音]";
                    break;
                case Video:
                    digest = "[视频]";
                    break;
                case Text:
                    TIMTextElem timTextElem = (TIMTextElem)element;
                    digest =  timTextElem.getText();
                    break;
                case File:
                    digest = "[文件]";
                    break;
                default:
                    Log.e("zjw", "error, unknow type");
            }
            message_tv.setText(digest);

            //时间
            time_tv.setText(DateUtils.timeMachineRecordTime(conversation.getLastMessage().timestamp()));

            //消息状态
//            if (lastMessage.direct() == EMMessage.Direct.SEND && lastMessage.status() == EMMessage.Status.FAIL) {
//                msg_state_iv.setVisibility(View.VISIBLE);
//            } else {
//                msg_state_iv.setVisibility(View.GONE);
//            }
        }

    }

    public interface OnItemRootClickListener {
        void onItemClick(View view, int position);
    }

    OnItemRootClickListener clickListener;

    public void setOnItemRootClickListener(OnItemRootClickListener clickListener) {
        this.clickListener = clickListener;
    }

}
